package org.lds.scheduled.job.admin.starter.holder;

import lombok.RequiredArgsConstructor;
import org.lds.scheduled.job.admin.business.worker.model.request.WorkerBaseListPageRequest;
import org.lds.scheduled.job.admin.business.worker.model.response.WorkerBaseResponse;
import org.lds.scheduled.job.admin.business.worker.service.WorkerBaseService;
import org.lds.scheduled.job.common.model.dto.ResultDTO;
import org.lds.scheduled.job.common.model.db.PageResult;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class GetValidWorkerStarter extends AbstractGetValidInstanceStarter<WorkerBaseResponse> {
    private final WorkerBaseService workerBaseService;
    private final WorkerHolder workerHolder;

    @Override
    protected void save(WorkerBaseResponse instance) {
        workerHolder.save(instance.getId(), instance.getAppId(), instance.buildAddress());
    }

    @Override
    protected Long id(WorkerBaseResponse instance) {
        return instance.getId();
    }

    @Override
    protected ResultDTO<PageResult<WorkerBaseResponse>> pageInstances(Long id) {
        WorkerBaseListPageRequest request = new WorkerBaseListPageRequest();
        request.setEnable(true);
        request.setGtId(id);
        request.setPageNum(1);
        request.setPageSize(500);
        request.setOrderByKey("id");
        request.setOrderByDesc(false);
        return workerBaseService.listWorkerPage(request);
    }
}
